﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using LoverMeeting.DataBase.Core;
using LoverMeeting.DataBase.DataMappers;

namespace LoverMeeting.DataBase
{
    public abstract class DaoRepositoryBase<TEntity> : SqlServerDao where TEntity : class
    {
        protected DaoRepositoryBase(IDataMapper<TEntity> mapper)
            : base(new SqlConnection(ConfigurationManager.ConnectionStrings["LoverMeetingConn"].ConnectionString))
        {
            Mapper = mapper;
        }

        protected int GetScopeId()
        {
            var command = new SqlCommand("SELECT @@Identity");
            var id = 0;
            try
            {
                command.Connection = SharedConnection;
                if (command.Connection != null && command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                var reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    id = int.Parse(reader[0].ToString());
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error populating data", e);
            }
            return id;
        }

        protected void Save(SqlCommand command)
        {
            try
            {
                command.Connection = SharedConnection;
                if (command.Connection != null && command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception("Error saving data", e);
            }
        }

        protected TEntity GetSingle(SqlCommand command)
        {
            TEntity entity = null;
            try
            {
                command.Connection = SharedConnection;
                if (command.Connection != null && command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                var reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    entity = Mapper.GetData(reader);
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error populating data", e);
            }
            finally
            {
                if (command.Connection != null) command.Connection.Close();
            }
            return entity;
        }

        protected List<TEntity> GetList(SqlCommand command)
        {
            var entityList = new List<TEntity>();
            try
            {
                command.Connection = SharedConnection;
                if (command.Connection != null && command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                var reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var entity = Mapper.GetData(reader);
                        entityList.Add(entity);
                    }
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error populating data", e);
            }
            finally
            {
                if (command.Connection != null) command.Connection.Close();
            }
            return entityList;
        }

        protected void CloseConnection()
        {
            if (SharedConnection != null && SharedConnection.State != ConnectionState.Open) 
                SharedConnection.Close();
        }

        protected IDataMapper<TEntity> Mapper;

    }
}
